package com.xuecheng.system.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.constants.UserConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.LoginDto;
import com.xuecheng.commons.model.vo.LoginVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.JwtUtils;
import com.xuecheng.system.domain.User;
import com.xuecheng.system.mapper.UserMapper;
import com.xuecheng.system.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户信息 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-12-01
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public ResponseResult<LoginVo> login(LoginDto loginDto) {
        //参数合法性校验
        String password = loginDto.getPassword();
        String username = loginDto.getUsername();
        String utype = loginDto.getUtype();
        if (StrUtil.isEmpty(username) || StrUtil.isEmpty(password)
                || StrUtil.isEmpty(utype)) {
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        //出行数据库校验用户名密码合法性
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getPhone,username);
        qw.eq(User::getUtype,utype);
        User user = this.getOne(qw);
        //账号校验
        if (user == null) {
            throw new BusinessException(ErrorCode.LOGINERROR);
        }
        //密码校验
        String md5 = SecureUtil.md5(password);
        if (!StrUtil.equals(md5, user.getPassword())) {
            throw new BusinessException(ErrorCode.LOGINERROR);
        }
        //登录成功以后，制作token
        Map claimMaps = new HashMap<>();
        claimMaps.put("userId",user.getId());
        claimMaps.put("companyId",user.getCompanyId());
        claimMaps.put("companyName",user.getCompanyName());
        String token = JwtUtils.createToken(claimMaps);
        //存入redis
        redisTemplate.opsForValue().set(UserConstants.USER_ID+user.getId(),user, Duration.ofHours(3));
        //封装结果
        LoginVo loginVo = new LoginVo();
        loginVo.setAccess_token(token);
        loginVo.setUsername(user.getPhone());
        //返回
        return ResponseResult.okResult(loginVo);
    }
    //修改用户密码
    @Override
    public ResponseResult changePwd(Map<String, String> map) {
        //获取请求体参数
        String password = map.get("password");
        String confirmPwd = map.get("confirmPwd");
        //参数合法性校验
        if (StrUtil.isEmpty(password) || StrUtil.isEmpty(confirmPwd)) {
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        //新密码md5加密
        String md5 = SecureUtil.md5(password);
        //修改用户密码
        this.lambdaUpdate().set(User::getPassword,md5)
                .eq(User::getId, AuthInfoHolder.getUserId())
                .update();
        //返回ok
        return ResponseResult.okResult();
    }
}
